package chapter1.service.collection;

import chapter1.domain.entity.Dept;
import chapter1.domain.entity.dto.DeptDto;
import chapter1.domain.entity.vo.DeptVo;
import chapter1.domain.entity.vo.Result;
import chapter1.service.IDeptService;
import chapter1.service.IUserService;
import chapter1.util.SingletonFactory;

import java.time.LocalDateTime;

/**
 * 业务层-部门
 *
 * @author 郑植航
 * @since 2025/4/18 15:20
 */
@Deprecated
public class DeptService extends BaseService<Dept,DeptDto,DeptVo> implements IDeptService {


    private IUserService userService;

    private DeptService(){
        temp = new DeptVo[0];
    }


    @Override
     DeptVo convertToVo(Dept dept){
        return new DeptVo(dept.getId(),dept.createAt,dept.updateAt,dept.name,dept.phone);
    }
    @Override
    Dept createEntity(DeptDto dto,int id){
        LocalDateTime now = LocalDateTime.now();
        return new Dept(id,now,now,dto.name, dto.phone);
    }
    @Override
    void updateEntity(DeptDto dto,Dept dept){
        dept.updateAt=LocalDateTime.now();
        dept.name=dto.name;
        dept.phone=dto.phone;
    }


    @Override
    public  Result delete(int id) {
        if (!existById(id)) {
            return Result.failure("删除失败，不存在该数据");
        }
        if(userService==null){
            userService= SingletonFactory.getInstance(UserService.class);
        }
        if(userService.deptInUse(id)){
            return Result.failure("删除失败，还有用户属于该部门，不能删除");
        }
        data.remove(id);
        return Result.success("删除成功", null);
    }
}
